package cn.botter.subject.common.context;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @BelongsProject: bo-club
 * @BelongsPackage: cn.botter.subject.application.context
 * @Author: 爱写Bug的Botter
 * @CreateTime: 2025-03-29:21:28
 * @Description: 描述
 * @Version: 1.0
 */
public class LoginContext {
    /**
     * 定义常量可继承本地线程，通过map中的key访问
     */
    private static final InheritableThreadLocal<Map<String , Object>> THREAD_LOCAL =
            new InheritableThreadLocal<>();

    public static void set (String key, Object value) {
        Map<String, Object> threadMap = getThreadMap();
        threadMap.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> threadMap = getThreadMap();
        return threadMap.get(key);
    }
    public static void remove(){
        THREAD_LOCAL.remove();
    }
    public static String getLoginId(){
        return (String) getThreadMap().get("loginId");
    }
    public static Map<String,Object> getThreadMap(){
        Map<String, Object> map = THREAD_LOCAL.get();
        if(Objects.isNull(map)){
            map = new ConcurrentHashMap<>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }
}
